using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GraphAlgsPresentation.Core
{
    /// <summary>
    /// The class provides the double buffer to help drawing smoothly
    /// </summary>
    public class DoubleBuffer
    {
        private Bitmap originalBackground = null;
        private Bitmap bufferBackground = null;
        private Bitmap doublebufferBackground = null;

        /// <summary>
        /// Creates the original background.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        public void CreateOriginalBackground(int width, int height)
        {
            if (originalBackground != null) originalBackground.Dispose();
            originalBackground = new Bitmap(width, height,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        }

        /// <summary>
        /// Resets the buffer background.
        /// </summary>
        public void ResetBufferBackground()
        {
            if (originalBackground != null)
                bufferBackground = (Bitmap)originalBackground.Clone();
        }

        /// <summary>
        /// Resets the double buffer background.
        /// </summary>
        public void ResetDoubleBufferBackground()
        {
            if (bufferBackground != null)
                doublebufferBackground = (Bitmap)bufferBackground.Clone();
        }

        /// <summary>
        /// Gets the buffer background graphics.
        /// </summary>
        /// <returns></returns>
        public Graphics GetBufferBackgroundGraphics()
        {
            if (bufferBackground == null) return null;
            Graphics g = Graphics.FromImage(bufferBackground);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            return g;
        }

        /// <summary>
        /// Gets the buffer background bitmap.
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBufferBackgroundBitmap()
        {
            return bufferBackground;
        }

        /// <summary>
        /// Gets the double buffer background graphics.
        /// </summary>
        /// <returns></returns>
        public Graphics GetDoubleBufferBackgroundGraphics()
        {
            if (doublebufferBackground == null) return null;
            Graphics g = Graphics.FromImage(doublebufferBackground);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            return g;
        }

        /// <summary>
        /// Gets the double buffer background bitmap.
        /// </summary>
        /// <returns></returns>
        public Bitmap GetDoubleBufferBackgroundBitmap()
        {
            return doublebufferBackground;
        }

        /// <summary>
        /// Draws the image.
        /// </summary>
        /// <param name="g">The graphics.</param>
        /// <param name="bitmap">The bitmap.</param>
        public void DrawImage(Graphics g, Bitmap bitmap)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawImage(bitmap, 0, 0);
        }
    }
}
